package org.ptkparty.client.view.games;

import java.util.Date;
import java.util.List;

import org.ptkparty.client.activity.ShowGamesActivity;
import org.ptkparty.client.resources.CellTableResource;
import org.ptkparty.shared.dto.GameDto;

import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.SafeHtmlCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class ShowGameView extends Composite {

	private static ShowGameViewUiBinder uiBinder = GWT.create(ShowGameViewUiBinder.class);

	interface ShowGameViewUiBinder extends UiBinder<Widget, ShowGameView> {
	}

	private ShowGamesActivity activity;
	private ListDataProvider<GameDto> provider;

	@UiField(provided = true)
	CellTable<GameDto> gamesTable;

	public ShowGameView() {
		gamesTable = new CellTable<GameDto>(Integer.MAX_VALUE, CellTableResource.INSTANCE);

		provider = new ListDataProvider<GameDto>();
		provider.addDataDisplay(gamesTable);

		final SingleSelectionModel<GameDto> selectionModel = new SingleSelectionModel<GameDto>();
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

			@Override
			public void onSelectionChange(SelectionChangeEvent event) {
				if (!selectionModel.getSelectedObject().getFinished()) {
					activity.showGame(selectionModel.getSelectedObject().getId());
				}
			}
		});
		gamesTable.setSelectionModel(selectionModel);

		ListHandler<GameDto> sorts = new ListHandler<GameDto>(provider.getList());
		gamesTable.addColumnSortHandler(sorts);

		Column<GameDto, String> status = new Column<GameDto, String>(new TextCell()) {

			@Override
			public String getValue(GameDto object) {
				if (object.getFinished()) {
					return "Terminé";
				} else {
					return "En cours";
				}
			}

		};
		gamesTable.addColumn(status, "Status");

		Column<GameDto, SafeHtml> winners = new Column<GameDto, SafeHtml>(new SafeHtmlCell()) {

			@Override
			public SafeHtml getValue(GameDto object) {
				SafeHtmlBuilder shb = new SafeHtmlBuilder();
				shb.append(SafeHtmlUtils.fromTrustedString("<b>")) //
						.append(object.getScoreWinner()) //
						.append(SafeHtmlUtils.fromTrustedString("</b>")) //
						.append(SafeHtmlUtils.fromString(" (" + object.getScaleWinner() + ")")) //
						.append(SafeHtmlUtils.fromTrustedString("<br/>"));

				for (int i = 0; i < object.getWinner().size(); i++) {
					if (i > 0) {
						shb.append(SafeHtmlUtils.fromTrustedString("<br/>"));
					}
					shb.append(SafeHtmlUtils.fromTrustedString(object.getWinner().get(i)));
				}
				return shb.toSafeHtml();
			}

		};
		gamesTable.addColumn(winners, "Gagnants");

		Column<GameDto, SafeHtml> loosers = new Column<GameDto, SafeHtml>(new SafeHtmlCell()) {

			@Override
			public SafeHtml getValue(GameDto object) {
				SafeHtmlBuilder shb = new SafeHtmlBuilder();
				shb.append(SafeHtmlUtils.fromTrustedString("<b>")) //
						.append(object.getScoreLooser()) //
						.append(SafeHtmlUtils.fromTrustedString("</b>")) //
						.append(SafeHtmlUtils.fromString(" (" + object.getScaleLooser() + ")")) //
						.append(SafeHtmlUtils.fromTrustedString("<br/>"));

				for (int i = 0; i < object.getLooser().size(); i++) {
					if (i > 0) {
						shb.append(SafeHtmlUtils.fromTrustedString("<br/>"));
					}
					shb.append(SafeHtmlUtils.fromTrustedString(object.getLooser().get(i)));
				}
				return shb.toSafeHtml();
			}

		};
		gamesTable.addColumn(loosers, "Perdants");

		Column<GameDto, Date> date = new Column<GameDto, Date>(new DateCell(DateTimeFormat.getFormat(PredefinedFormat.DATE_TIME_MEDIUM))) {

			@Override
			public Date getValue(GameDto object) {
				return object.getDateCreation();
			}

		};
		gamesTable.addColumn(date, "Date");

		Column<GameDto, String> creator = new Column<GameDto, String>(new TextCell()) {

			@Override
			public String getValue(GameDto object) {
				return object.getCreator();
			}

		};
		gamesTable.addColumn(creator, "Créateur");
		
		Column<GameDto, String> place = new Column<GameDto, String>(new TextCell()) {

			@Override
			public String getValue(GameDto object) {
				return object.getPlace();
			}

		};
		gamesTable.addColumn(place, "Boulodrome");
		
		final ButtonCell btnCell = new ButtonCell() {
			@Override
			public void render(com.google.gwt.cell.client.Cell.Context context, SafeHtml data, SafeHtmlBuilder sb) {
				if (((GameDto) context.getKey()).getFinished() && context.getIndex() != 0) {
					sb.appendHtmlConstant("<button class=\"disabled error\" type=\"button\" tabindex=\"-1\">");
				} else {
					sb.appendHtmlConstant("<button class=\"error\" type=\"button\" tabindex=\"-1\">");
				}
				if (data != null) {
					sb.append(data);
				}
				sb.appendHtmlConstant("</button>");
			}
		};
		Column<GameDto, String> delete = new Column<GameDto, String>(btnCell) {

			@Override
			public String getValue(GameDto arg0) {
				return "Suppr.";
			}

		};
		delete.setFieldUpdater(new FieldUpdater<GameDto, String>() {

			@Override
			public void update(int arg0, GameDto arg1, String arg2) {
				if (!arg1.getFinished() || arg0 == 0) {
					activity.deleteGame(arg1);
				}
			}
		});

		gamesTable.addColumn(delete, "");

		initWidget(uiBinder.createAndBindUi(this));
	}

	public void setPresenter(ShowGamesActivity showGamesActivity) {
		activity = showGamesActivity;
	}

	public void loadData(List<GameDto> result) {
		provider.setList(result);
		provider.refresh();
	}

}
